/*
 * Powered By [Frank-Liz-Lee]
 * Copyright(C) 2012-2016 Liz Company
 * All rights reserved.
 */

package com.linln.modules.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.system.mapper.UserMapper;
import com.linln.modules.system.domain.User;
import java.util.List;

import com.linln.modules.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Transactional(rollbackFor = Exception.class)
@Service
public class UserServiceImpl  extends GenericService<User, Long>  implements UserService {

    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /**
     * 通过主键查询实体对象
     * @param primaryKey
     * @return
     * @
     */
    public User getByPK(Long primaryKey)  {
        return userMapper.getByPK(primaryKey);
    }

    /**
     * 查询所有记录
     * @return
     * @
     */
    public List<User> list()  {
        return userMapper.list();
    }

    /**
     * 根据查询条件查询所有记录
     * @return
     * @
     */
    public List<User> listByProperty(User user) {
        return userMapper.listByProperty(user);
    }


    /**
     * 根据主键删除记录
     * @param primaryKey
     * @return
     * @
     */
    public int deleteByPK(Long primaryKey)  {
        return userMapper.deleteByPK(primaryKey);
    }

    /**
     * 根据多个主键删除记录
     * @param primaryKeys
     * @
     */
    public int deleteByPKeys(List<Long> primaryKeys)  {
        return userMapper.deleteByPKeys(primaryKeys);
    }

    /**
     * 根据传入参数删除记录
     * @param user
     * @return
     * @
     */
    public int deleteByProperty(User user)  {
        return userMapper.deleteByProperty(user);
    }

    /**
     * 保存记录
     * @param user
     * @return
     * @
     */
    public int save(User user)  {
        return userMapper.save(user);
    }

    /**
     * 更新记录
     * @param user
     * @return
     * @
     */
    public int update(User user) {
        return userMapper.update(user);
    }

    /**
     * 根据条件查询记录条数
     * @param user
     * @return
     * @
     */
    public int findByCount(User user)  {
        return userMapper.findByCount(user);
    }

    /**
     * 根据查询条件查询分页记录
     * @return
     */
    public PageInfo<User> findByPage(Integer pageNum, Integer pageSize, User user) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList = userMapper.listByProperty(user);
        if(null == userList || userList.size() == 0){
            return new Page<User>().toPageInfo();
        }
        Page<User> page =  (Page<User>)userList;
            return page.toPageInfo();
    }

    @Override
    public boolean updateStatus(StatusEnum statusEnum, List<Long> ids) {
        if(null == statusEnum || CollectionUtils.isEmpty(ids)){
            return false;
        }
        int result =  userMapper.updateStatus(statusEnum.getCode(),ids);
        return result > 0;
    }

    /**
     * 用戶是否存在
     * @param query
     * @return
     */
    @Override
    public boolean repeatByUsername(User query) {

        if(StringUtil.isEmpty(query.getUsername())){
            return false;
        }
        User user = new User();
        user.setUsername(query.getUsername());
        List<User> userList = userMapper.listByProperty(user);

        user = CollectionUtils.isEmpty(userList)  ? null  : userList.get(0);
        if(null == user){
            //根据用户名查不到用户，说明没有重复的
            return false;
        }

        if(query.getId() == null){
            return user != null;//没有传递id进来，表示新增的，但已有用户名
        }
        //传递id进来，表示编辑的，可以修改用户名(只要不与别的用户信息重复用户名即可)
        return !user.getId().equals(query.getId());

    }


    @Override
    public User getByUsername(String username) {
        if(StringUtil.isEmpty(username)){
            return null;
        }
        User user = new User();
        user.setUsername(username);
        List<User> userList = userMapper.listByProperty(user);
        return CollectionUtils.isEmpty(userList)  ? null  : userList.get(0);
    }

    @Override
    public List<User> getUserList(Long roleId) {
        return userMapper.getUserList(roleId,StatusEnum.OK.getCode());
    }
}
